var MY_CLINIC_DPT = (function() {
	function load() {
		var $tbody = $('table#dptList>tbody');
		$tbody.empty();
		WeKnow.loading();
		var dpts = MY_CLINIC_DPT_COMMON.getDpts();
		if (dpts && dpts.length) {
			var e = '';
			$(dpts).each(function(idx, dpt) {
				e += '<tr data-dptcd="' + dpt.dptCd + '" data-dptname="' + dpt.dptName + '" data-ordernb="' + (dpt.orderNb != null ? dpt.orderNb : "") + '">';
					e += '<td>' + dpt.dptCd + '</td>';
					e += '<td>' + dpt.dptName + '</td>';
					e += '<td>' + (dpt.orderNb != null ? dpt.orderNb : "") + '</td>';
					e += '<td>';
						e += '<button type="button" class="btn btn-sm btn-link text-danger" name="remove">删除</button>';
						e += '<button type="button" class="btn btn-sm btn-link" name="edit" style="margin-left: 15px;">编辑</button>';
					e += '</td>';
				e += '</tr>';
			});
			$(e).appendTo($tbody);
		}
		
		WeKnow.closeLoading();
	}
	
	return {
		load: load
	}
})();

$(document).ready(function() {
	
	// 新增
	$('button#add').on('click', function() {
		var $dptModal = $('div#dptModal');
		$dptModal.modal('show');
		
		$dptModal.find('input[type="text"]').val('');
		$dptModal.find('input#dptCd').attr('readonly', false);
	});
	// 编辑
	$('table#dptList').on('click', 'button[name="edit"]', function() {
		var $dptInfo = $(this).closest('tr');
		var $dptModal = $('div#dptModal');
		$dptModal.modal('show');
		
		$dptModal.find('input#dptCd').attr('readonly', true);
		$dptModal.find('input#dptCd').val($dptInfo.data('dptcd'));
		$dptModal.find('input#dptName').val($dptInfo.data('dptname'));
		$dptModal.find('input#orderNb').val($dptInfo.data('ordernb'));
	});
	
	// 删除
	$('table#dptList').on('click', 'button[name="remove"]', function() {
		
		var $dptInfo = $(this).closest('tr');
		
		WeKnow.confirm('确定删除？', function() {
			WeKnow.loading();
			$.ajax({ 
				type: "POST", 
				url: "wncrm_clinic_dpt.do/remove", 
				dataType: "json",
				data: {
					dptCd: $dptInfo.data('dptcd')
				},
				async: false,
				success: function(data) {
					if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
					if (data.errcode == 0) {
						$dptInfo.remove();
						$('div#dptModal').modal('hide');
						WeKnow.successMsg('删除成功');
					} else if (data.errcode == 1) {
						WeKnow.notifyMsg('科室下面有医生，请前往“员工管理”移除该科室下面的医生再删除');
					} else {
						WeKnow.systemMsg("系统异常");
					}
				},
				error: function() {
					WeKnow.systemMsg("系统异常");
				}
			});
			WeKnow.closeLoading();
		});
	});
	
	// 保存信息
	$('button#saveOrUpdate').on('click', function() {
		
		var params = {}; //参数
		
		var $dptCd = $('input#dptCd');
		if ($.trim($dptCd.val()).length) {
			params.dptCd = $.trim($dptCd.val());
			
			if ($dptCd.attr('readonly') == 'readonly') {
				params.sqlAction = 'U'; // 更新
			} else {
				params.sqlAction = 'C'; // 新增
			}
		} else {
			WeKnow.errorMsg('科室编号必须');
			$dptCd.focus();
			return false;
		}
		
		var $dptName = $('input#dptName');
		if ($.trim($dptName.val()).length) {
			params.dptName = $.trim($dptName.val());
		} else {
			WeKnow.errorMsg('科室名称必须');
			$dptName.focus();
			return false;
		}
		
		var $orderNb = $('input#orderNb');
		var orderNb = $.trim($('input#orderNb').val());
		if (orderNb.length) {
			if (!MY_VALIDATOR.isNumber(orderNb)) {
				WeKnow.errorMsg('显示顺序必须是数字');
				$orderNb.focus();
				return false;
			} else {
				params.orderNb = orderNb;
			}
		}
		
		WeKnow.loading();
		$.ajax({ 
			type: "POST", 
			url: "wncrm_clinic_dpt.do/saveOrUpdate", 
			dataType: "json",
			data: params,
			async: false,
			success: function(data) {
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
				if (data.errcode == 0) {
					$('div#dptModal').modal('hide');
					MY_CLINIC_DPT.load();
					WeKnow.successMsg('保存成功');
				} else if (data.errcode == 1) {
					WeKnow.errorMsg('科室编号已经存在，请更换');
				} else {
					WeKnow.systemMsg("系统异常");
				}
			},
			error: function() {
				WeKnow.systemMsg("系统异常");
			}
		});
		WeKnow.closeLoading();
	});
	
	MY_CLINIC_DPT.load();
});